package javase.collections;

import java.util.Arrays;
import java.util.List;

public class ArrayAsListExample {

	public static void main(String[] args) {
		Integer[] array = initIntArray();
		printIntArray(array);
		List<Integer> list = Arrays.asList(array); // does not returns actual list, only list-view of array
		printList(list);
//		illegal modification
		
//		list.add(Integer.valueOf(123));
//		list.remove(0);
		array[0] = new Integer(999);

		printList(list);
		printIntArray(array);
	}
	
	private static void printList(List<Integer> list) {
	    System.out.println("List = " + list);
	}

	private static Integer[] initIntArray() {
	    int arrayCapacity = 10;
	    Integer[] array = new Integer[arrayCapacity];
	    for(int i = 0; i < arrayCapacity; i++) {
		array[i] = Integer.valueOf(i);
	    }
	    return array;
	}
	
	private static void printIntArray(Integer [] array) {
	    StringBuilder arrayAsString = new StringBuilder("Array = [");
	    for (int i = 0; i < array.length; i++) {
		arrayAsString.append(array[i]);
		if (i + 1 < array.length) {
		    arrayAsString.append(", ");
		}
	    }
	    arrayAsString.append("]");
	    System.out.println(arrayAsString.toString());
	}
}
